#include "pch.h"
#include "Texture2D.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include "Common.h"

using namespace hiveFX;

CTexture2D* CTexture2D::loadTexture(const std::string &vTexturePath)
{
	int Width, Height, Channels;
	unsigned char* pData = stbi_load(vTexturePath.c_str(), &Width, &Height, &Channels, 0);
	if (pData == nullptr)
	{
		LOG_ERROR(hiveFX::TAG_KEYWORD::TEXTURE2D_TAG, "Failed to load texture asset: {}", vTexturePath);
		return nullptr;
	}

	GLint Format = GL_RGB;
	if (Channels == 3) Format = GL_RGB;
	else if (Channels == 4) Format = GL_RGBA;
	else if (Channels == 1) Format = GL_RED;

	GLuint TextureHandle;
	glGenTextures(1, &TextureHandle);
	glBindTexture(GL_TEXTURE_2D, TextureHandle);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexImage2D(GL_TEXTURE_2D, 0, Format, Width, Height, 0, Format, GL_UNSIGNED_BYTE, pData);
	glGenerateMipmap(GL_TEXTURE_2D);

	bool IsValid = (glIsTexture(TextureHandle) == GL_TRUE);
	if (!IsValid)
	{
		LOG_ERROR(hiveFX::TAG_KEYWORD::TEXTURE2D_TAG, "Failed to create texture: {}", vTexturePath);
		return nullptr;
	}
	stbi_image_free(pData);

	return new CTexture2D(TextureHandle);
}

CTexture2D::~CTexture2D()
{
	glDeleteTextures(1, &m_TextureHandle);
	m_TextureHandle = 0;
}

void hiveFX::CTexture2D::bindTexture() const
{
	glBindTexture(GL_TEXTURE_2D, m_TextureHandle);
}

CTexture2D::CTexture2D(GLuint TextureHandle) : m_TextureHandle(TextureHandle) {}
